package com.aaa.vegetablemarketexpress.service.impl;

import com.aaa.vegetablemarketexpress.dao.SpecDao;
import com.aaa.vegetablemarketexpress.entity.Spec;
import com.aaa.vegetablemarketexpress.entity.User;
import com.aaa.vegetablemarketexpress.service.SpecService;
import com.aaa.vegetablemarketexpress.util.CustomException;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.ResultStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @FileName: SpecController
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/9/6 15:51
 * @Version: 1.0.0
 */
@Service
public class SpecServiceImpl implements SpecService {
    @Resource
    private SpecDao specDao;
    @Resource
    private HttpSession session; // 获取session

    /**
     * 分页
     * @param page
     * @return
     */
    @Override
    public PageInfo<Spec> queryByPage(Page<Spec> page) {
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();
        if (pageNo == null || pageSize == null||pageNo == 0||pageSize == 0) {
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(), ResultStatus.PARAM_ISNULL.getStatusMsg());
        }
        // 调用PageHelper设置参数方法，开始分页
        PageHelper.startPage(pageNo,pageSize);
        // 获取page类的 data 属性，该属性 是Dept类型
        Spec data = page.getData();
        // 调用dao层查询
        List<Spec> specList = specDao.queryByParam(data);
        // 使用查询结果封装成返回对象
        return new PageInfo(specList);
    }

    /**
     * id查询
     * @param specId
     * @return
     */
    @Override
    public Spec queryBySpecId(Long specId) {
      return specDao.queryBySpecId(specId);
    }

    /**
     * 添加
     * @param spec
     * @return
     */
    @Override
    public int add(Spec spec) {
        spec.setStatus("0");
        spec.setDelFlag("0");
        spec.setCreateTime(new Date());
        User userList = (User)session.getAttribute("userInfo");
        String userName = userList.getUserName();
        spec.setCreateBy(userName);
        return specDao.add(spec);
    }

    /**
     * 修改
     * @param spec
     * @return
     */
    @Override
    public int update(Spec spec) {
        spec.setUpdateTime(new Date());
        User userList = (User)session.getAttribute("userInfo");
        String userName = userList.getUserName();
        spec.setUpdateBy(userName);
        return specDao.update(spec);
    }

    /**
     * 删除
     * @param specId
     * @return
     */
    @Override
    public int deleteBySpecId(Long specId) {
        return  specDao.deleteBySpecId(specId);
    }
}
